package com.yueze.demo1.controller;


import com.yueze.demo1.entities.Image1;
import com.yueze.demo1.entities.User;
import com.yueze.demo1.imgtools.ImgTools;
import com.yueze.demo1.service.Image1Service;
import com.yueze.demo1.util.MyCopy;
import com.yueze.demo1.util.WaterMark;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.*;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

/**
 * @author Administrator
 * @create 2020-07-27
 * 10:51
 * @desc
 **/
@Controller
public class UpdateImages {

    private static InetAddress localHost;
    static {
        try {
            localHost = Inet4Address.getLocalHost();
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
            System.out.println(localHost);
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


    @Resource
    private Image1Service image1Service;

    @RequestMapping("/index/update")
    public String update(@RequestParam("image1") MultipartFile image1,
                         HttpSession session , Model model,
                         HttpServletRequest request, HttpServletResponse response) {
        Image1 img = new Image1();
        User user = (User) session.getAttribute("user");
        img.setUid(user.getId());
        String suffix = image1.getContentType().toLowerCase();//图片后缀，用以识别哪种格式数据
        suffix = suffix.substring(suffix.lastIndexOf("/") + 1);

        if (suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("png") || suffix.equals("gif")) {
            String fileName = "1"+UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
            String fileName2 = "2"+fileName;
            //String imgFilePath = "C:"+File.separator+"javaproject"+File.separator+"siruan"+File.separator+"save"+File.separator;
            String imgFilePath = File.separator+"hzsr"+File.separator+"web"+File.separator+"save"+File.separator;//新生成的图片
            File targetFile = new File(imgFilePath, fileName);

            if (!targetFile.getParentFile().exists()) { //注意，判断父级路径是否存在
                targetFile.getParentFile().mkdirs();
            }
            //保存
            try {
                image1.transferTo(targetFile);
                System.out.println(imgFilePath+fileName);
               MyCopy.save(imgFilePath+fileName,imgFilePath+fileName2);
               img.setImg_size(targetFile.length()+"");
                img.setImg_size2(targetFile.length()+"");
            } catch (Exception e) {
                e.printStackTrace();
            }
            img.setImg_name2(fileName2);
            String reception = imgFilePath +fileName2;
            model.addAttribute("path1",fileName);
            session.setAttribute("path2",reception);
            session.setAttribute("name",fileName2);
            session.setAttribute("path1",fileName);
            session.setAttribute("url",imgFilePath);
            session.setAttribute("suffix",suffix);
            img.setImg_url("http://192.168.2.141/shuiyin/images/"+fileName);
            img.setImg_name(fileName);
            session.setAttribute("img",img);
            model.addAttribute("img",img);
        }
        image1Service.create1(img);

        return "opertation";
    }

    @RequestMapping("/index/waterMark")
    public String waterMark(Model model,HttpSession session,String jjj){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        System.out.println("===================================================");
        String fileName = (String) session.getAttribute("path2");
        String fileName1 = (String) session.getAttribute("path1");
        Image1 img = (Image1) session.getAttribute("img");

        System.out.println(fileName+"11111111111111111111");
        if (fileName!=null){
            WaterMark waterMark = new WaterMark();
            if (jjj == null){
                jjj = "@杭州思软";
            }
            boolean b = waterMark.createMark(fileName, jjj, Color.red, 70f, "");
            System.out.println(b+"---------------------------");
        }else {

        }
        String name = (String) session.getAttribute("name");
        img.setImg_name2(name);
        image1Service.create2(img);
        model.addAttribute("path2",name);
        model.addAttribute("path1",fileName1);
        model.addAttribute("img",img);
        return "opertation";
    }

    @RequestMapping("/index/yasuo")
    public String yasuo(HttpSession session,HttpServletRequest request, Model model){
        String thumbImg = null;//压缩过的图片名称
/*        model.addAttribute("path1",fileName);
        session.setAttribute("path2",reception);
        session.setAttribute("name",fileName2);
        session.setAttribute("path1",fileName);
        session.setAttribute("url",imgFilePath);*/
        Image1 sessionImg = (Image1) session.getAttribute("img");
        String fileName = (String) session.getAttribute("path1");
        String fileName2 = (String) session.getAttribute("name");
        String imgFilePath = (String) session.getAttribute("url");
        String suffix = (String) session.getAttribute("suffix");
        if (fileName != null) {
            //压缩图片
            ImgTools img = new ImgTools();
            try {
                thumbImg = img.tools(imgFilePath, fileName, suffix);
                File file = new File(imgFilePath, thumbImg);
                long length = file.length();
                sessionImg.setImg_size3(length+"");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //用request传值到前端
        //request.setAttribute("fileName", fileName);
        sessionImg.setImg_name3(thumbImg);
        image1Service.create3(sessionImg);
        request.setAttribute("thumbImg", thumbImg);
        request.setAttribute("sessionImg", sessionImg);
        model.addAttribute("sessionImg",sessionImg);
        model.addAttribute("path2",fileName2);
        model.addAttribute("path1",fileName);
        return "opertation";
    }


    @RequestMapping("/index/select")
    public String selectAll(Model model,HttpSession session){

        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<Image1> imgList = image1Service.selectAll(id);

        model.addAttribute("imgList",imgList);
        return "index2";
    }
    @RequestMapping("/index/wenjian")
    public String wenjian(String img_name,Model model,HttpSession session){
        System.out.println(img_name);
        System.out.println("============== @RequestMapping=================");
        Image1 img = image1Service.getImageById(img_name);
        String fileName = img.getImg_name();
        String fileName2 = img.getImg_name2();
        String imgFilePath = img.getImg_url();
        System.out.println("============== String imgFilePath = img.getImg_url();g=================");
        session.setAttribute("path2",imgFilePath+fileName2);
        session.setAttribute("name",fileName2);
        session.setAttribute("path1",fileName);
        session.setAttribute("url",imgFilePath);
        model.addAttribute("path1",fileName);
        session.setAttribute("img",img);
        model.addAttribute("img",img);

        return "opertation";
    }

    @RequestMapping("/index")
    public String index(){
        return "opertation";
    }

    @RequestMapping("/index/del")
    public String del(String img_name){

        System.out.println(img_name);
        image1Service.delete(img_name);
        return "redirect:/index/select";
    }
}
